// 再次封装，接收返回的promise数据
import {getDayData, getMonthData, getYearData} from '@/services/request.js'
import { formatChsDate } from '@/libs/utils.js'

const getData = async (store, field, date) => {
  let data = null
  console.log(field,date,12121212)
  switch(field) {
    case 'day': 
      data = await getDayData(date)
      break;
    case 'month': 
      data = await getMonthData(date)
      break;
    case 'year': 
      data = await getYearData(date)
      break;
    default:
      data = await getDayData(date)
      break
  }
  console.log(data)
  store.commit('setErrorCode', data.error_code)   //存储错误码

  let res = null
  if (data.error_code != 0) {
    return
  }
  switch(field) {
    case 'day': 
      res = data.result.data 
      res.date = formatChsDate(res.date, 'day')
      res['year-month'] = formatChsDate(res['year-month'], 'month')
      break
    case 'month':
      res = data.result.data.holiday_array
      for (let item of res) {
        item.festival = formatChsDate(item.festival, 'day')
      }
      break

    case 'year' :
      res = data.result.data.holiday_list
      for (let item of res) {
        item.startday = formatChsDate(item.startday, 'day')
      }
      break
  }
  
  store.commit('setData',{
    field,
    data: res
  })
}

export default getData